package yunwo.cn.yzstation.view.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.AssembleLvadapter;
import yunwo.cn.yzstation.api.IassembleView;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.CarListBean;
import yunwo.cn.yzstation.presenter.AssembleActivityPersenter;

/**
 * Created by lao on 2017/9/2.
 */
public class AssembleActivity extends MyBaseActivity implements View.OnClickListener,IassembleView {

    private ListView mAssemble_lv;
    private XRefreshView mXrefreshview;
    private ImageView mXinzeng;
    private Intent mIntent;
    private AssembleActivityPersenter mAssembleActivityPersenter;
    int votes = 0;
    int Weight = 0;
    double Volumes;
    int Number = 0;
    private TextView mVotes;
    private TextView mWeight;
    private TextView mVolumes;
    private TextView mNumber;
    private AssembleLvadapter mAssembleLvadapter;
    CarListBean.DataBean mDataBean;
    int cout = 1;
    private List<CarListBean.DataBean.ListBean> mList;
    boolean isend = false;
    private ProgressDialog mMyDialog;
    private String mMain;
    private String mType = "";
    private LinearLayout mZhuangtai;
    private TextView mZhuangtaiText;
    private ImageView mZhuangtaiIMG;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mMyDialog == null) {
            mMyDialog = ProgressDialog.show(AssembleActivity.this, null, "请稍等...");
            mMyDialog.setCancelable(false);
            mMyDialog.show();
        }
        mType = getIntent().getStringExtra("main") == null ? "" : getIntent().getStringExtra("main");

        mAssembleActivityPersenter = new AssembleActivityPersenter(this,this);
        initView();

    }



    private void initView() {

        mXinzeng = (ImageView) findViewById(R.id.xinzeng);
        mAssemble_lv = (ListView) findViewById(R.id.assemble_lv);
        mXrefreshview = (XRefreshView) findViewById(R.id.xrefreshview);
        mVotes = (TextView) findViewById(R.id.votes);
        mWeight = (TextView) findViewById(R.id.Weight);
        mVolumes = (TextView) findViewById(R.id.Volumes);
        mNumber = (TextView) findViewById(R.id.Number);



        mZhuangtai = (LinearLayout) findViewById(R.id.zhuangtai);
        mZhuangtaiText = (TextView) findViewById(R.id.zhuangtaiText);
        mZhuangtaiIMG = (ImageView) findViewById(R.id.zhuangtaiIMG);

        mAssembleLvadapter = new AssembleLvadapter(AssembleActivity.this,this,mType);
        mXrefreshview.setPullLoadEnable(true);
        mXrefreshview.setPullRefreshEnable(true);
        mXrefreshview.setAutoLoadMore(false);
        mXrefreshview.setScrollBackDuration(300);
        mXinzeng.setOnClickListener(this);
        mAssembleActivityPersenter.getlistData("",1,mType);
        mXrefreshview.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                mAssembleActivityPersenter.getlistData("1",1,mType);
//                mAllFragmentPersenter.getData("1",1,5);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
             /*   if (!isend) {
                    cout += 1;
                    mAssembleActivityPersenter.getlistData("2",cout);
                }else {
                    Toast.makeText(AssembleActivity.this, "已加载全部数据", Toast.LENGTH_SHORT).show();
                    mXrefreshview.stopLoadMore(true);
                }*/
                if (mDataBean!=null &&mDataBean.isHasNextPage()) {
                    cout += 1;
                    mAssembleActivityPersenter.getlistData("2",cout,mType);
//                    mAllFragmentPersenter.getData("2",cout,5);
                    mXrefreshview.stopLoadMore();
                }else {
                    Toast.makeText(AssembleActivity.this, "已加载全部数据", Toast.LENGTH_SHORT).show();
                    mXrefreshview.stopLoadMore(true);
                }

            }
        });
    }

    @Override
    protected String settiltile() {
        mType = getIntent().getStringExtra("main") == null ? "" : getIntent().getStringExtra("main");

        if (mType.equals("1")) {
            return "短驳列表";
        } else {
            return "干线列表";
        }


    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_assemble;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.xinzeng:
                mIntent = new Intent(AssembleActivity.this, AssignmentActivity.class);
                mIntent.putExtra("type", mType);
                startActivity(mIntent);
                break;
        }
    }

    @Override
    public void getlistDataSuccess(CarListBean.DataBean dataBean,String type) {


        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }

        this.mDataBean = dataBean;
        mList = dataBean.getList();
        if (dataBean.getList().size() == 0) {

            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.VISIBLE);
            }
        }else {
            if (mZhuangtai != null) {
                mZhuangtai.setVisibility(View.GONE);
            }
        }


        switch (type) {
            case "":
                mAssembleLvadapter.refreshDataAndNotifyDataSetChanged(mList);
                mAssemble_lv.setAdapter(mAssembleLvadapter);
                break;
            case "1":
                if (mXrefreshview != null) {
                    mXrefreshview.stopRefresh();
                    cout = 1;
                    if (dataBean.getList().size() == 0) {
                        mAssemble_lv.setVisibility(View.INVISIBLE);
                        Toast.makeText(AssembleActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();
                        mAssembleLvadapter.refreshDataAndNotifyDataSetChanged(dataBean.getList());
                    }else {
                        Toast.makeText(AssembleActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                    }
                    mAssembleLvadapter.refreshDataAndNotifyDataSetChanged(mList);
                    mAssemble_lv.setAdapter(mAssembleLvadapter);
                }
                break;
            case "2":
                if (mXrefreshview != null) {
                    mXrefreshview.stopLoadMore();
                    List<CarListBean.DataBean.ListBean> list = dataBean.getList();
                    Toast.makeText(AssembleActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
                    mList.addAll(list);
                    mAssembleLvadapter.addDataAndNotifyDataSetChanged(list);

                }
        }


        for (int i = 0; i < mList.size(); i++) {
            votes = votes + mList.get(i).getVotes();
            Weight = Weight + mList.get(i).getWeight();
            Volumes = Volumes + mList.get(i).getVolume();
            Number = Number + mList.get(i).getGoodsNumber();
        }
        mVotes.setText(votes + "");
        mWeight.setText(Weight + "KG");
        mVolumes.setText(Volumes + "");
        mNumber.setText(Number + "");



    }

    @Override
    public void getlistDatafailure() {
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }

        mXrefreshview.stopRefresh();
        mXrefreshview.stopLoadMore();
    }

    @Override
    public void departSueccss() {
        mAssembleActivityPersenter.getlistData("1",1,mType);
    }
}
